<template>
  <div>
    <div v-if="isLoggedIn" class="user-info-container">
      <el-avatar :size="60" :src="userAvatar" class="user-avatar" @click="goToProfile" />
      <div class="user-details">
        <span class="user-name">{{ userName }}</span>
        <el-button type="text" size="small" class="logout-button" @click="logout">退出登录</el-button>
      </div>
    </div>
    <div v-else>
      <router-link to="/login" class="login-link">
        <el-button type="primary" class="custom-login-button">登录</el-button>
      </router-link> 
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { userStore } from '../store/UserStore'

const store = userStore()
const router = useRouter()

const isLoggedIn = computed(() => store.getLoginStatus)
const userInfo = computed(() => store.getUserInfo)
const userName = computed(() => userInfo.value ? userInfo.value.userName || '用户' : '用户')
// 将 userAvatar 固定为指定图片
const userAvatar = 'https://youke1.picui.cn/s1/2025/07/16/687749b3baf85.jpg';

const logout = () => {
  store.logout()
  router.push('/login')
}

const goToProfile = () => {
  if (isLoggedIn.value) {
    router.push('/user-profile')
  }
}
</script>

<style scoped>
.user-info-container {
  display: flex;
  align-items: center;
  background: linear-gradient(135deg, #e0f7fa, #b2ebf2); /* 渐变背景 */
  padding: 15px;
  border-radius: 12px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease; /* 过渡效果 */
}

.user-info-container:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}

.user-avatar {
  margin-right: 20px;
  border: 3px solid #fff;
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  transition: transform 0.3s ease;
}

.user-avatar:hover {
  transform: scale(1.1);
}

.user-details {
  display: flex;
  flex-direction: column;
}

.user-name {
  font-size: 18px;
  font-weight: 700;
  color: #212121;
  margin-bottom: 5px;
}

.logout-button {
  color: #f44336;
  transition: color 0.3s ease;
}

.logout-button:hover {
  color: #d32f2f;
}

.custom-login-button {
  background-color: #2196f3;
  border-color: #2196f3;
  color: white;
  font-size: 16px;
  font-weight: 600;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, transform 0.3s ease;
}

.custom-login-button:hover {
  background-color: #1976d2;
  border-color: #1976d2;
  transform: translateY(-2px);
}

.login-link {
  text-decoration: none;
}
</style>